﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Text.RegularExpressions;
using IronOne.SalesOne.BusinessObjects;


    public class MenuMap : StaticSiteMapProvider
    {
        Facade.StructureFacade structureFacade = new Facade.StructureFacade();

        private SiteMapNode parentNode
        {
            get;
            set;
        }

        public override SiteMapNode BuildSiteMap()
        {
            lock (this)
            {
                parentNode = HttpContext.Current.Cache["SiteMap"] as SiteMapNode;
                if (parentNode == null)
                {


                    base.Clear();
                    //parentNode = new SiteMapNode(this,
                    //                        HttpRuntime.AppDomainAppVirtualPath,
                    //                        HttpRuntime.AppDomainAppVirtualPath + "Default.aspx",
                    //                        "Home");
                    parentNode = new SiteMapNode(this,
                                            HttpRuntime.AppDomainAppVirtualPath,
                                             "index.aspx",
                                            "Home");

                    AddNode(parentNode);
                    //AddFiles(parentNode);
                    AddUrls(parentNode);

                    HttpContext.Current.Cache.Insert("SiteMap", parentNode);
                }
                return parentNode;
            }
        }

        private void AddUrls(SiteMapNode parentNode)
        {
          
            List<Module> modules = structureFacade.GetModules("ModuleId ASC");

            foreach (Module module in modules)
            {
                SiteMapNode moduleNode = new SiteMapNode(this,
                                    "http://www.google.lk/search?hl=en&q=" + module.ModuleName,
                                    module.ModuleName,
                                    module.ModuleName,
                                    module.ModuleName);

                AddNode(moduleNode, parentNode);

                module.SubTasks = structureFacade.GetTasksForModule(module);
                foreach (Task task in module.SubTasks)
                {
                    if (!task.Url.Contains("index"))
                    {
                        SiteMapNode taskNode = new SiteMapNode(this,
                               "http://www.google.lk/search?hl=en&q="+task.TaskName,
                               task.TaskName,
                               task.TaskName,
                               task.TaskName);
                        AddNode(taskNode, moduleNode);
                        List<IronOne.SalesOne.BusinessObjects.Page> pages = structureFacade.GetPagesForTask(task);

                        foreach (IronOne.SalesOne.BusinessObjects.Page page in pages)
                        {

                            SiteMapNode pageNode = new SiteMapNode(this,
                                                   page.Url,
                                                   page.Url,
                                                   module.ModuleName,
                                                   page.Title);
                            AddNode(pageNode, parentNode);
                        }
                    }

                }
            }
        }

        private void AddFiles(SiteMapNode folderNode)
        {
            //var files = from o in Directory.GetFiles(HttpContext.Current.Server.MapPath(folderNode.Key))
            //            let fileName = new FileInfo(o)
            //            select new
            //            {
            //                FileName = fileName.Name
            //            };

            //foreach (var item in files)
            //{
            //    SiteMapNode fileNode = new SiteMapNode(this,
            //                        item.FileName,
            //                        folderNode.Key + "/" + item.FileName,
            //                        item.FileName);
            //    AddNode(fileNode, folderNode);
            //}
        }

        protected override SiteMapNode GetRootNodeCore()
        {
            return BuildSiteMap();
        }
}
